package com.kaikeba.wechat.controller;

import com.kaikeba.bean.Courier;
import com.kaikeba.bean.Message;
import com.kaikeba.bean.User;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.service.CourierService;
import com.kaikeba.service.UserService;
import com.kaikeba.service.impl.CourierServiceImpl;
import com.kaikeba.service.impl.UserServiceImpl;
import com.kaikeba.util.JsonUtil;
import com.kaikeba.util.UserSessionUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class WXController {

    private UserService userService = new UserServiceImpl();
    private CourierService courierService = new CourierServiceImpl();

    @ResponseBody("/wx/register.do")
    public String register(HttpServletRequest req, HttpServletResponse resp){
        resp.setContentType("text/html;charset=utf-8");
        String userPhone = req.getParameter("phone");
        String password = req.getParameter("password");
        String userName = "待完善";
        String identityNumber = "待完善";
        User user = new User(userName,userPhone,identityNumber,password);
        boolean flag = userService.insert(user);
        Message message = new Message();
        if (flag){
            message.setStatus(0);
            message.setResult("注册成功");
        }else {
            message.setStatus(-1);
            message.setResult("抱歉，注册失败");
        }
        String json = JsonUtil.toJson(message);
        return json;
    }

    @ResponseBody("/wx/login.do")
    public String login(HttpServletRequest req, HttpServletResponse resp){
        resp.setContentType("text/html;charset=utf-8");
        String userPhone = req.getParameter("userPhone");
        String password = req.getParameter("password");
        Courier courier = courierService.login(userPhone, password);
        if (courier!=null){
            //快递员身份
            Message message = new Message();
            message.setResult("登录成功");
            message.setStatus(0);
            UserSessionUtil.setCourier(req.getSession(),courier);
            String json = JsonUtil.toJson(message);
            System.out.println(json);
            return json;
        }else {
            //普通用户身份
            User user = userService.login(userPhone, password);
            Message message = new Message();
            if (user!=null){
                //添加用户标识
                user.setUser(true);
                //登录成功
                //将查到用户放入session
                UserSessionUtil.setUser(req.getSession(),user);
                message.setStatus(0);
                message.setResult("登录成功");
            }else {
                //登录失败
                message.setStatus(-1);
                message.setResult("登录失败");
            }
            String json = JsonUtil.toJson(message);
            return json;
        }
    }

    @ResponseBody("/wx/logout.do")
    public String logOut(HttpServletRequest req, HttpServletResponse resp){
        req.getSession().invalidate();
        Message message = new Message();
        message.setStatus(0);
        return JsonUtil.toJson(message);
    }

    /**
     * 用于判断登录用户身份，从而决定是否显示快递助手此模块
     * @param req
     * @param resp
     * @return
     */
    @ResponseBody("/wx/helper.do")
    public String helper(HttpServletRequest req, HttpServletResponse resp){
        User user = UserSessionUtil.getUser(req.getSession());
        Message message = new Message();
        if (user!=null){
            if (user.isUser()){
                //用户登录
                message.setStatus(0);
            }
        }else {
            message.setStatus(-1);
        }


        String s = JsonUtil.toJson(message);
        System.out.println(s);
        return s;

    }


}
